package org.budo.ehcache.replicator.factory;

import org.budo.ehcache.replicator.EhcacheReplicator;
import org.budo.ehcache.replicator.ReplicatorMessage;
import org.budo.ehcache.replicator.util.EhcacheReplicatorUtil;
import org.budo.support.spring.util.SpringUtil;
import org.springframework.cache.interceptor.CacheEvictOperation;

import net.sf.ehcache.CacheException;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.event.CacheEventListener;

/**
 * @author lmw
 */
public class EhcacheReplicatorEventListener implements CacheEventListener {
    /**
     * Spring的clear操作走的是EhCache的removeAll方法
     * 
     * @see org.springframework.cache.ehcache.EhCacheCache#evict(Object)
     * @see org.springframework.cache.interceptor.CacheAspectSupport#performCacheEvict(CacheOperationContext,CacheEvictOperation,Object)
     */
    @Override
    public void notifyRemoveAll(Ehcache cache) {
        EhcacheReplicator replicator = SpringUtil.getBeanCached(EhcacheReplicator.class);
        if (null == replicator) {
            EhcacheReplicatorUtil.logError("#45 notifyRemoveAll error, EhcacheRedisReplicator.getInstance() is null, cache=" + cache);
            return;
        }

        ReplicatorMessage message = EhcacheReplicatorUtil.newReplicatorMessage(cache, null, EhcacheReplicatorUtil.NOTIFY_REMOVE_ALL);
        replicator.publishReplicatorMessage(message);
    }

    /**
     * 单个缓存过期事件
     */
    @Override
    public void notifyElementRemoved(Ehcache cache, Element element) throws CacheException {
        EhcacheReplicator replicator = SpringUtil.getBeanCached(EhcacheReplicator.class);
        if (null == replicator) {
            EhcacheReplicatorUtil.logError("#65 notifyElementRemoved error, EhcacheRedisReplicator.getInstance() is null, cache=" + cache + ", element=" + element);
            return;
        }

        ReplicatorMessage message = EhcacheReplicatorUtil.newReplicatorMessage(cache, element, EhcacheReplicatorUtil.NOTIFY_ELEMENT_REMOVED);
        replicator.publishReplicatorMessage(message);
    }

    /**
     * 无处理
     */
    @Override
    public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException {
        //
    }

    /**
     * 无处理
     */
    @Override
    public void notifyElementPut(Ehcache cache, Element element) throws CacheException {
        //
    }

    /**
     * 过期应该各管各的，不同步
     */
    @Override
    public void notifyElementExpired(Ehcache cache, Element element) {
        //
    }

    /**
     * Spring的evict操作走的是EhCache的remove方法
     * 
     * @see org.springframework.cache.ehcache.EhCacheCache#evict(Object)
     */
    @Override
    public void notifyElementEvicted(Ehcache cache, Element element) {
        //
    }

    /**
     * 接口方法空实现
     */
    @Override
    public void dispose() {
        //
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}